﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Gift.DAL
{
    /// <summary>
    /// 数据访问类:DALStockOrder
    /// </summary>
    public partial class DALStockOrder
    {
        public DALStockOrder()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tbStockOrder");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Gift.Model.tbStockOrder model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tbStockOrder(");
            strSql.Append("stockOrderCode,companyId,stockCompanyId,stockStates,stockTime,stockStartPersion,creater,createTime,address,goodsToDate,linker,linkerPhone,remark)");
            strSql.Append(" values (");
            strSql.Append("@stockOrderCode,@companyId,@stockCompanyId,@stockStates,@stockTime,@stockStartPersion,@creater,@createTime,@address,@goodsToDate,@linker,@linkerPhone,@remark)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@stockOrderCode", SqlDbType.VarChar,50),
					new SqlParameter("@companyId", SqlDbType.Int,4),
					new SqlParameter("@stockCompanyId", SqlDbType.Int,4),
					new SqlParameter("@stockStates", SqlDbType.Int,4),
					new SqlParameter("@stockTime", SqlDbType.DateTime),
					new SqlParameter("@stockStartPersion", SqlDbType.VarChar,50),
					new SqlParameter("@creater", SqlDbType.Int,4),
					new SqlParameter("@createTime", SqlDbType.DateTime),
					new SqlParameter("@address", SqlDbType.NVarChar,300),
					new SqlParameter("@goodsToDate", SqlDbType.DateTime),
					new SqlParameter("@linker", SqlDbType.NVarChar,50),
					new SqlParameter("@linkerPhone", SqlDbType.NVarChar,50),
					new SqlParameter("@remark", SqlDbType.NVarChar,500)};
            parameters[0].Value = model.stockOrderCode;
            parameters[1].Value = model.companyId;
            parameters[2].Value = model.stockCompanyId;
            parameters[3].Value = model.stockStates;
            parameters[4].Value = model.stockTime;
            parameters[5].Value = model.stockStartPersion;
            parameters[6].Value = model.creater;
            parameters[7].Value = model.createTime;
            parameters[8].Value = model.address;
            parameters[9].Value = model.goodsToDate;
            parameters[10].Value = model.linker;
            parameters[11].Value = model.linkerPhone;
            parameters[12].Value = model.remark;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int UpdateStockGoodsFreeze(string stockOrderCode)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tbGoodsFreeze(goodsId,freezeCount,orderCode,modelTypeid)");
            strSql.Append(" select goodsId,goodsCount,stockOrderCode,modelTypeId from tbStockOrderGoods where stockOrderCode=@stockOrderCode  ");

            SqlParameter[] parameters = {
					new SqlParameter("@stockOrderCode", SqlDbType.VarChar,50)};

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Gift.Model.tbStockOrder model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tbStockOrder set ");
            strSql.Append("stockOrderCode=@stockOrderCode,");
            strSql.Append("companyId=@companyId,");
            strSql.Append("stockCompanyId=@stockCompanyId,");
            strSql.Append("stockStates=@stockStates,");
            strSql.Append("stockTime=@stockTime,");
            strSql.Append("stockStartPersion=@stockStartPersion,");
            strSql.Append("creater=@creater,");
            strSql.Append("createTime=@createTime,");
            strSql.Append("address=@address,");
            strSql.Append("goodsToDate=@goodsToDate,");
            strSql.Append("linker=@linker,");
            strSql.Append("linkerPhone=@linkerPhone,");
            strSql.Append("remark=@remark");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@stockOrderCode", SqlDbType.VarChar,50),
					new SqlParameter("@companyId", SqlDbType.Int,4),
					new SqlParameter("@stockCompanyId", SqlDbType.Int,4),
					new SqlParameter("@stockStates", SqlDbType.Int,4),
					new SqlParameter("@stockTime", SqlDbType.DateTime),
					new SqlParameter("@stockStartPersion", SqlDbType.VarChar,50),
					new SqlParameter("@creater", SqlDbType.Int,4),
					new SqlParameter("@createTime", SqlDbType.DateTime),
					new SqlParameter("@address", SqlDbType.NVarChar,300),
					new SqlParameter("@goodsToDate", SqlDbType.DateTime),
					new SqlParameter("@linker", SqlDbType.NVarChar,50),
					new SqlParameter("@linkerPhone", SqlDbType.NVarChar,50),
					new SqlParameter("@remark", SqlDbType.NVarChar,500),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = model.stockOrderCode;
            parameters[1].Value = model.companyId;
            parameters[2].Value = model.stockCompanyId;
            parameters[3].Value = model.stockStates;
            parameters[4].Value = model.stockTime;
            parameters[5].Value = model.stockStartPersion;
            parameters[6].Value = model.creater;
            parameters[7].Value = model.createTime;
            parameters[8].Value = model.address;
            parameters[9].Value = model.goodsToDate;
            parameters[10].Value = model.linker;
            parameters[11].Value = model.linkerPhone;
            parameters[12].Value = model.remark;
            parameters[13].Value = model.Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbStockOrder ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbStockOrder ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gift.Model.tbStockOrder GetModel(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,stockOrderCode,companyId,stockCompanyId,stockStates,stockTime,stockStartPersion,creater,createTime,address,goodsToDate,linker,linkerPhone,remark from tbStockOrder ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            Gift.Model.tbStockOrder model = new Gift.Model.tbStockOrder();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["stockOrderCode"] != null && ds.Tables[0].Rows[0]["stockOrderCode"].ToString() != "")
                {
                    model.stockOrderCode = ds.Tables[0].Rows[0]["stockOrderCode"].ToString();
                }
                if (ds.Tables[0].Rows[0]["companyId"] != null && ds.Tables[0].Rows[0]["companyId"].ToString() != "")
                {
                    model.companyId = int.Parse(ds.Tables[0].Rows[0]["companyId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["stockCompanyId"] != null && ds.Tables[0].Rows[0]["stockCompanyId"].ToString() != "")
                {
                    model.stockCompanyId = int.Parse(ds.Tables[0].Rows[0]["stockCompanyId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["stockStates"] != null && ds.Tables[0].Rows[0]["stockStates"].ToString() != "")
                {
                    model.stockStates = int.Parse(ds.Tables[0].Rows[0]["stockStates"].ToString());
                }
                if (ds.Tables[0].Rows[0]["stockTime"] != null && ds.Tables[0].Rows[0]["stockTime"].ToString() != "")
                {
                    model.stockTime = DateTime.Parse(ds.Tables[0].Rows[0]["stockTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["stockStartPersion"] != null && ds.Tables[0].Rows[0]["stockStartPersion"].ToString() != "")
                {
                    model.stockStartPersion = ds.Tables[0].Rows[0]["stockStartPersion"].ToString();
                }
                if (ds.Tables[0].Rows[0]["creater"] != null && ds.Tables[0].Rows[0]["creater"].ToString() != "")
                {
                    model.creater = int.Parse(ds.Tables[0].Rows[0]["creater"].ToString());
                }
                if (ds.Tables[0].Rows[0]["createTime"] != null && ds.Tables[0].Rows[0]["createTime"].ToString() != "")
                {
                    model.createTime = DateTime.Parse(ds.Tables[0].Rows[0]["createTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["address"] != null && ds.Tables[0].Rows[0]["address"].ToString() != "")
                {
                    model.address = ds.Tables[0].Rows[0]["address"].ToString();
                }
                if (ds.Tables[0].Rows[0]["goodsToDate"] != null && ds.Tables[0].Rows[0]["goodsToDate"].ToString() != "")
                {
                    model.goodsToDate = DateTime.Parse(ds.Tables[0].Rows[0]["goodsToDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["linker"] != null && ds.Tables[0].Rows[0]["linker"].ToString() != "")
                {
                    model.linker = ds.Tables[0].Rows[0]["linker"].ToString();
                }
                if (ds.Tables[0].Rows[0]["linkerPhone"] != null && ds.Tables[0].Rows[0]["linkerPhone"].ToString() != "")
                {
                    model.linkerPhone = ds.Tables[0].Rows[0]["linkerPhone"].ToString();
                }
                if (ds.Tables[0].Rows[0]["remark"] != null && ds.Tables[0].Rows[0]["remark"].ToString() != "")
                {
                    model.remark = ds.Tables[0].Rows[0]["remark"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
          
            strSql.Append("select Id,stockOrderCode,companyId,stockCompanyId,stockStates,stockTime,stockStartPersion,creater,createTime,address,goodsToDate,linker,linkerPhone,remark ");
            strSql.Append(", (select sum( goodsCount * goodsStockPrice) from tbStockOrderGoods where stockOrderCode = tbStockOrder.stockOrderCode) as sumOrderPrice ");
            strSql.Append(",isnull((select [name] from tbCompany where id=tbStockOrder.companyId),'') as companyName,isnull((select [name] from tbCompany where id=tbStockOrder.stockCompanyId),'') as stockCompanyName "); 
            strSql.Append(" FROM tbStockOrder ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        public void UpdateStockOutOrderStates(string sStockOrderCode, int orderId, int orderType)
        {
            StringBuilder strUpdate = new StringBuilder();
            strUpdate.Append("update tbStockOrder set stockStates=2 where stockOrderCode=@stockOrderCode ");
            strUpdate.Append(" and (select sum(goodsCount) from tbStockOrderGoods where stockOrderCode=@stockOrderCode ) = (select sum(isnull(goodsCount,0)) from tbWarehouseGoodsOut where orderId=@orderId and orderType=@orderType ); ");

            strUpdate.Append("update tbStockOrder set stockStates=1 where stockOrderCode=@stockOrderCode ");
            strUpdate.Append(" and (select sum(goodsCount) from tbStockOrderGoods where stockOrderCode=@stockOrderCode ) > (select sum(isnull(goodsCount,0)) from tbWarehouseGoodsOut where orderId=@orderId and orderType=@orderType ); ");

            SqlParameter[] parameters = {
					new SqlParameter("@stockOrderCode", SqlDbType.VarChar,50),
                    	new SqlParameter("@orderId", SqlDbType.Int,4),
                        	new SqlParameter("@orderType", SqlDbType.Int,4)

			};
            parameters[0].Value =  sStockOrderCode;
            parameters[1].Value = orderId;
            parameters[2].Value = orderType;

            DbHelperSQL.Query(strUpdate.ToString(), parameters);
        }

        public void UpdateStockOrderStates(string sStockOrderCode)
        {
            StringBuilder strUpdate = new StringBuilder();
            strUpdate.Append("update tbStockOrder set stockStates=2 where stockOrderCode=@stockOrderCode ");
            strUpdate.Append(" and (select sum(goodsCount) from tbStockOrderGoods where stockOrderCode=@stockOrderCode ) = (select sum(isnull(inWarehouseCount,0)) from tbStockOrderGoods where stockOrderCode=@stockOrderCode ); ");

            strUpdate.Append("update tbStockOrder set stockStates=1 where stockOrderCode=@stockOrderCode ");
            strUpdate.Append(" and (select sum(goodsCount) from tbStockOrderGoods where stockOrderCode=@stockOrderCode ) > (select sum(isnull(inWarehouseCount,0)) from tbStockOrderGoods where stockOrderCode=@stockOrderCode ) and (select sum(isnull(inWarehouseCount,0)) from tbStockOrderGoods where stockOrderCode=@stockOrderCode ) >0; ");

            SqlParameter[] parameters = {
					new SqlParameter("@stockOrderCode", SqlDbType.VarChar,50)
			};
            parameters[0].Value = sStockOrderCode;

            DbHelperSQL.Query(strUpdate.ToString(), parameters);
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" Id,stockOrderCode,companyId,stockCompanyId,stockStates,stockTime,stockStartPersion,creater,createTime,address,goodsToDate,linker,linkerPhone,remark ");
            strSql.Append(" FROM tbStockOrder ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tbStockOrder ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.Id desc");
            }
            strSql.Append(")AS Row, T.*  from tbStockOrder T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "tbStockOrder";
            parameters[1].Value = "Id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

